﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Build.BuildEngine;

namespace SetCommonConfigOnTargets
{
	public class AssemblyInfoFixFilter : Filter
	{
		#region Filter Members

		public bool Match(System.IO.FileInfo file)
		{
			if(!file.FullName.EndsWith("AssemblyInfo.cs"))
				return false;

			Project project = new Project();
			project.Load(file.Directory.Parent.GetFiles("*.csproj").First().FullName);
			var outDir = project.GetEvaluatedProperty("OutDir");
			if(outDir != "..\\bin" && outDir != "..\\bin\\")
				return false;
			return true;
		}

		public void Process(System.IO.FileInfo file)
		{
			string[] linesToDelete = new[]{
			"[assembly: AssemblyDescription(\"\")]\r\n[assembly: AssemblyConfiguration(\"\")]\r\n[assembly: AssemblyCompany(\"\")]\r\n",
			"[assembly: AssemblyCopyright(\"Copyright ©  2009\")]\r\n[assembly: AssemblyTrademark(\"\")]\r\n[assembly: AssemblyCulture(\"\")]\r\n",

			"[assembly: ComVisible(false)]",

			"[assembly: AssemblyVersion(\"1.0.0.0\")]\r\n[assembly: AssemblyFileVersion(\"1.0.0.0\")]"};
			var text = File.ReadAllText(file.FullName);
			var originalText = text;
			foreach(var line in linesToDelete)
			{
				text = text.Replace(line, "");
			}
			if(text != originalText)
				File.WriteAllText(file.FullName, text);
		}

		#endregion
	}
}
